<?php
class wapi_mod_base {
	public $db;
	public $class = '';
	public $has_focus = false;
	public $firstrun = false;
	public $firstrun_user = false;
	public $firstrun_session = false;
	public $run_cnt = 0;
	private $xsession_vars = array();
	private $session_vars = array();

	function __construct(){
		global $wapi;
		$this->class = get_class($this);
		$last_run_cnt = $wapi->xget('run_cnt',$this->class);
		if (!$last_run_cnt){
			$this->firstrun = true;
			$this->firstrun_user = true;
			$this->firstrun_session = true;
			$last_run_cnt = 0;
		}
		$this->db = $wapi->get_app_db($this->class,false);
		$last_run_cnt ++;
		$this->run_cnt = $last_run_cnt;
		$wapi->xset('run_cnt',$this->run_cnt,$this->class);
		if (method_exists($this,'_construct'))
			$this->_construct();
	}
    public function __get($name){
    	global $wapi;
        $fn_name = 'get_' . $name;
        if (method_exists($this, $fn_name)){
            return $this->$fn_name();
        } else {
			if (in_array($name,$this->session_vars)){
				$svar = $this->class . '_' . $name;
				if (isset($_SESSION[$svar])){
					return $_SESSION[$svar];
				}
			} elseif (in_array($name,$this->xsession_vars)){
				return $wapi->xget($name,$this->class);
			}
        }
        return null;
    }
    public function __set($name, $value){
    	global $wapi;
        $fn_name = 'set_' . $name;
        if (method_exists($this, $fn_name)){
            $this->$fn_name($value);
            return true;
        } else {
			if (in_array($name,$this->session_vars)){
				$svar = $this->class . '_' . $name;
				$_SESSION[$svar] = $value;
				return true;
			} elseif (in_array($name,$this->xsession_vars)){
				$wapi->xset($name,$value,$this->class);
				return true;
			}
        }
        return false;
    }
    // + cross session variable functions
    public function xset($var_name,$var_val) {
    	global $wapi;
    	return $wapi->xset($var_name,$var_val,$this->class);
    }
    public function xget($var_name) {
    	global $wapi;
    	return $wapi->xget($var_name,$this->class);
    }
    public function xunset($var_name) {
    	global $wapi;
    	return $wapi->xunset($var_name,$this->class);
    }
    public function user_xset($var_name,$var_val) {
    	global $wapi;
    	return $wapi->user_xset($var_name,$var_val,$this->class);
    }
    public function user_xget($var_name) {
    	global $wapi;
    	return $wapi->user_xget($var_name,$this->class);
    }
    public function user_xunset($var_name) {
    	global $wapi;
    	return $wapi->user_xunset($var_name,$this->class);
    }
    // - cross session variable functions
}
?>